package cn.itcast.itcaststore.web.servlet;

import cn.itcast.itcaststore.domain.Products;
import cn.itcast.itcaststore.service.Impl.IProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/AddCardServlet")
public class AddCardServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        HttpSession session = request.getSession();
        //购物车对象
        ArrayList card = (ArrayList) session.getAttribute("card");
        IProductService iProductService = new IProductService();


        if (card==null){
            card=new ArrayList();
        }

        ArrayList  products = new ArrayList<>();

        Products product=iProductService.getProductsById(id);
        //添加商品信息
        products.add(product);
        //添加购买商品数量
        products.add(1);

        card.add(products);

        session.setAttribute("card",card);
        response.sendRedirect(request.getContextPath()+"/FindProductServlet?id="+id);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
